package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.meituan.OrderStatusInfo;
import com.yami.shop.bean.model.Order;
import com.yami.shop.dao.OrderStatusInfoMapper;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.OrderStatusInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderStatusInfoServiceImpl extends ServiceImpl<OrderStatusInfoMapper, OrderStatusInfo> implements OrderStatusInfoService {
    @Autowired
    private OrderStatusInfoMapper orderStatusInfoMapper;
    @Autowired
    private OrderStatusInfoService orderStatusInfoService;
    @Autowired
    private OrderService orderService;


    @Override
    public OrderStatusInfo getPeiSongId(String orderNumber) {
        return orderStatusInfoMapper.getPeiSongId(orderNumber);
    }

    @Override
    @Transactional
    public Map dealOrderStatus(OrderStatusInfo orderStatusInfo) {
        Map map = new HashMap();
        if(orderStatusInfo!=null){
            orderStatusInfo.setCreateTime(new Date());
            orderStatusInfoService.saveOrUpdate(orderStatusInfo);
            //根据返回的订单状态,修改订单的状态
            String status = orderStatusInfo.getStatus();
            //获取订单编号,修改订单状态
            String orderId = orderStatusInfo.getOrderId();
            Order order = null;
            if(orderId!=null) {
                 order = orderService.getOrderByOrderNumber(orderId);
                if(status.equals("20")||status.equals("30")){
                    order.setStatus(3);
                }else if(status.equals("50")){
                    order.setStatus(5);
                }
                orderService.saveOrUpdate(order);
            }

        }
        map.put("code","0");
        return map;
    }
}
